<template>
    <div class="courses-container">
        <el-card style="max-width: 1000vh">
            <template #header ><span style="font-size: 22px;margin-left: 20px;font-weight: 700">我的授课</span></template>
            <div v-if="courses.length === 0" class="no-courses-message">
                您还没有任何授课课程!
            </div>
            <a v-for="(course, index) in courses" :key="course.id" @click="navigateToCourse(course.id)" class="course-card">
                <el-card  shadow="hover">
                    <div class="card-content">
                        <image-preview :src="course.imgs" class="course-image"/>
                        <div class="course-details">
                            <h2 class="course-title">{{ course.name }}</h2>
                            <p class="course-description">{{ course.coursedesc }}</p>
                            <div class="course-meta">
                                <span class="course-date"><el-icon><Timer /></el-icon>{{ course.starttime }} - {{ course.endtime }}</span>
                                <span class="course-teacher"><el-icon><Avatar /></el-icon>选课人数 {{ course.nownum }}</span>
                            </div>
                        </div>
                    </div>
                </el-card>
            </a>
        </el-card>
    </div>
</template>

<script setup >
    import { onMounted, ref } from 'vue';
    import { RouterLink,useRouter } from 'vue-router';
    import { listMyTeacherCourse } from '@/api/MyTeacherCourse/MyTeacherCourse';

    const courses = ref([]);
    onMounted(() => {
        const userId = localStorage.getItem('userId'); // 从localStorage获取用户ID
        listMyTeacherCourse({ userId}).then(response => {
            courses.value = response.rows;
        });
    });
    // 初始化 router 实例
    const router = useRouter();
    /** 页面跳转*/
    function navigateToCourse(id) {
        router.push(`/MyTeacherCourses/${id}`);
    }

</script>


<style scoped>
    .courses-container {
        max-width: 80%; /* 调整宽度 */
        margin: auto;
        padding: 20px;
    }
    .no-courses-message {
        text-align: center;
        font-size: 1.2em;
        color: #888;
        margin-top: 50px;
    }

    .title {
        text-align: center;
        margin-bottom: 40px;
    }

    .course-card {
        display: block; /* 修改为块级元素 */
        width: 100%; /* 占据全部宽度 */
        text-decoration: none;
        color: inherit;
    }

    .card-content {
        display: flex;
        align-items: center;
        padding: 20px;
        background-color: #f9f9f9;
        border-radius: 6px;
        box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
        margin: 8px;
    }

    .course-image {
        width: 400px;
        height: 35vh;
        object-fit: cover;
        border-radius: 6px;
        margin-right: 20px;
    }

    .course-details {
        flex-grow: 1;
        max-width: 600px;
    }

    .course-title {
        font-size: 1.4em;
        font-weight: bold; /* 加粗标题 */
        margin-bottom: 10px;
    }

    .course-description {
        line-height: 1.6;
        margin-bottom: 10px;

    }

    .course-meta {
        display: flex;
        gap: 10px;
        margin-top: 10px;
    }


    .course-date,
    .course-teacher {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        padding: 2px 4px;
        border-radius: 4px;
        font-size: 0.7em;
        color: #fff;
    }

    .course-date {
        background-color: #88c8f5;
    }

    .course-teacher {
        /*background-color: #e1be5b;*/
        background-color: #d3a184;
    }
</style>